var request = require('superagent-charset');
var superagent = require('superagent');
var cheerio = require('cheerio');
var async = require("async");
var fs = require("fs");
var mysql = require("mysql");
request(superagent);

//连接数据库
var connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: 'root',
    database: 'node',
});
connection.connect();



var url = "http://www.37zw.net/5/5783/";
superagent.get(url).charset('gbk').end(function(err, mes) {
    if (err) {
        console.log("出问题了1:" + err);
    } else {
        var $ = cheerio.load(mes.text);

        var title = $('h1').text();
        var txtle = $('#intro').text();
        var sid = url.split('/')[4];
        var images = 'http://www.37zw.net' + $('#fmimg').find('img').attr('src');

        //写入书本列表
        var userinto = 'INSERT INTO booklist(name,txtle,sid,images,links) VALUES(?,?,?,?,?)';
        var userinto_Params = [title, txtle, sid, images, url];
        connection.query(userinto, userinto_Params, function(err, result) {
            if (err) {
                console.log('[ERROR] - ', err.message);
                return;
            } else {
                console.log("成功加入booklist");
            }
        });

        var xiao = [];
        $("#list dd").each(function() {
            var _title = $(this).find("a").text();
            var _href = url + $(this).find("a").attr("href");
            xiao.push(_href);
        });

        //创建小说表
        connection.query("create table book"+sid+"(id int auto_increment primary key,title varchar(255),cont text)", function(err, result) {
            if (err) {
                throw err } else {
                console.log("创建表成功")
            }
        })

        //小说列表页爬取
        var bb = [];
        var _len = xiao.length;
        var _k = 0;
        console.log(_len+"章");
        async.mapLimit(xiao, 1, function(url, callback) {
            _k++;
            superagent.get(url).charset('gbk').end(function(err, mes) {
                if (err) {
                    console.log("出问题了2:" + err);
                } else {
                    var $ = cheerio.load(mes.text);
                    var _cont = $("#content").text();
                    var _title = $("h1").text();

                    var userinto = 'INSERT INTO book'+sid+'(title,cont) VALUES(?,?)';
                    var userinto_Params = [_title,_cont];
                    connection.query(userinto, userinto_Params, function(err, result) {
                        if (err) {
                            console.log('[ERROR] - ', err.message);
                            return;
                        } else {
                            console.log(_title+"章节成功加入");
                        }
                    });


                };
                callback(null);
            });

        }, function(error, results) {
            console.log("result :");
            console.log(results);
        });




    };



});
